﻿@inherits Vowei.Web.G18nWebViewPage<Vowei.Core.Models.ICommentable>
@using Vowei.Data.Helpers
@using Vowei.Web.Helpers

<script type="text/javascript" src="@Url.Content("~/Scripts/tinymce/tiny_mce.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/tinymce/jquery.tinymce.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/json2.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/htmlEncode.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/date.format.js")"></script>

<script language="javascript" type="text/javascript">
    var annotation_catalog_url = '/Comment/Create';

    // TODO: 这一段函数需要执行安全性测试，直接是将Html发送过去了
    function annotation_save_handler() {
        // TODO: 需要测试，网页上已经有一个tinyMCE，而且名称也是note_editor的情况
        if (tinyMCE.get('note_editor')) {
            var html = tinyMCE.get('note_editor').getContent();

            var comment = {
                Text: html,
                PostId: "@Model.ID",
                PostType: "@EntityHelpers.GetEntityTypeName(Model.GetType())",
                User: "@Context.User.Identity.Name"
            };

            $.ajax({
                url: annotation_catalog_url,
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(comment),
                contentType: 'application/json; charset=utf-8',
                success: function (data) {
                    tinyMCE.get('note_editor').setContent('');
                    var date = eval(data.CommentDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
                    var tmpl = "<div class='display-comment'><strong>" + data.User + "</strong> - <strong>" + dateFormat(date,"yyyy/mm/dd HH:MM:ss") + "</strong><p>" + data.Text + "</p></div>";
                    $(tmpl).prependTo('#comment_list');
                }
            });
        }
    }

    $(document).ready(function () {
        $("#note_editor").tinymce({
            mode: "textareas",
            theme: "simple"
        });

        $('#create_comment').click(function () {
            annotation_save_handler();
        });
    });
</script>

<fieldset id="comment_list">
    <legend>@T("评论")</legend>
        
    @* TODO: 评论应该按评论时间顺序进行反向排序 *@
    @foreach (var comment in Model.Comments.OrderByDescending(c => c.CommentDate))
 {
     var text = new HtmlString(comment.Text);
            <div class="display-comment">
                <strong>@comment.User</strong> - <strong>@comment.CommentDate</strong>
                <p>
                @text
                </p>
            </div>
    }
</fieldset>
    
<div>
    <fieldset>
        <legend>@T("添加新评论")</legend>
                
        <div id="note_editor">
            &nbsp;
        </div>
        <p>
            <input type="button" id="create_comment" value="@T("提交")" />
        </p>
    </fieldset>
</div>

<div id="result">
</div>